﻿$(document).ready(function () {
    jobPackageChanged();
    $("#JobPackageID").change(function () {
        jobPackageChanged();
    });

    $("#payment-form").submit(function (event) {
        // disable the submit button to prevent repeated clicks
        $('#submit-button').attr("disabled", "disabled");
        $("#processingHint").show();
        // createToken returns immediately - the supplied callback submits the form if there are no errors
        Stripe.createToken({
            number: $('.card-number').val(),
            cvc: $('.card-cvc').val(),
            exp_month: $('.card-expiry-month').val(),
            exp_year: $('.card-expiry-year').val()
        }, stripeResponseHandler);
        return false; // submit from callback
    });
});

function stripeResponseHandler(status, response) {
    if (response.error) {
        // re-enable the submit button
        $('#submit-button').removeAttr("disabled");
        $("#processingHint").hide();
        var message = response.error.message;

        if (response.error.param == "exp_year") {
            message = "Your card's expiration year is invalid";
        }
        else if (response.error.param == "exp_month") {
            message = "Your card's expiration month is invalid";
        }
        else if (response.error.param == "cvc") {
            message = "Your card's security code is invalid";
        }
        else if (response.error.param == "number") {
            message = "Your card's number is invalid";
        }
        // show the errors on the form
        $("#card-error").html(message);
    } else {
        var form$ = $("#payment-form");
        // token contains id, last4, and card type
        var token = response['id'];
        // insert the token into the form so it gets submitted to the server
        form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
        //ajax submit payment form
        $("#payment-form").ajaxSubmit({
            beforeSubmit: function (a, f, o) {
                //alert("before");
            },
            dataType: 'json',
            success: ProcessPayment,
            error: function (request, status, errorThrown) {     // error during save, display error
                //alert(request.responseText);
                return false;
            }
        });
    }
}

function ProcessPayment(data) {
    JsonResultHandler.Success(data);
    if (data.Success == 0) {
        $('#submit-button').removeAttr("disabled");
        $("#processingHint").hide();
    }
}

function jobPackageChanged() {
    var html = "<em>You are not picking a job package</em>";

    var jobPackageID = parseInt($("#JobPackageID").attr("value"));
    if ($.isNumeric(jobPackageID) && jobPackageID >= 1 && jobPackageID <= 7) {
        var htmlTemplate = 'You are picking <strong>{0} Package</strong>, and <strong>US ${1}</strong> will be charged upon purchase. <div class="smallspacer"></div>You will get <strong>{2}</strong> job credits and remaining <strong>{3}</strong> job credits will be added into your account after posting this listing. <div class="smallspacer"></div><em>* Job credits never expire.</em>'
        if (isBuyCreditsPage()) {
            htmlTemplate = 'You are picking <strong>{0} Package</strong>, and <strong>US ${1}</strong> will be charged upon purchase. <div class="smallspacer"></div>You will get <strong>{2}</strong> job credits. <div class="smallspacer"></div><em>* Job credits never expire.</em>';
        }
        var packageName = "";
        var total = "";
        var credits = "";
        var creditsLeft = "";

        if (jobPackageID == 1) {
            packageName = "Single-Post";
            total = "99";
            credits = "1";
            creditsLeft = "0";
        }
        else if (jobPackageID == 2) {
            packageName = "3-Posts";
            total = "267";
            credits = "3";
            creditsLeft = "2";
        }
        else if (jobPackageID == 3) {
            packageName = "5-Posts";
            total = "395";
            credits = "5";
            creditsLeft = "4";
        }
        else if (jobPackageID == 4) {
            packageName = "10-Posts";
            total = "690";
            credits = "10";
            creditsLeft = "9";
        }
        else if (jobPackageID == 5) {
            packageName = "20-Posts";
            total = "1180";
            credits = "20";
            creditsLeft = "19";
        }
        else if (jobPackageID == 6) {
            packageName = "50-Posts";
            total = "2450";
            credits = "50";
            creditsLeft = "49";
        }
        else if (jobPackageID == 7) {
            packageName = "100-Posts";
            total = "3900";
            credits = "100";
            creditsLeft = "99";
        }

        html = htmlTemplate.replace("{0}", packageName).replace("{1}", total).replace("{2}", credits).replace("{3}", creditsLeft);
    }
    $("#orderInfo").html(html);
}

function isBuyCreditsPage() {
    var href = window.location.href.toLowerCase();
    if (href.indexOf("/job/buycredits") >= 0) {
        return true;
    }
    else {
        return false;
    }
}